var zfgl_app = function(){
	//积分
	function initJf(){
		var tabHeadRactive = new Ractive({
			el: '.hp-container',
            template: '#zfgl-template',
            oncomplete: function () {
                
                $(".js-tabs li").click(function() {
            		$(".js-tabs li").removeClass("active");
            		$(this).addClass("active");
            		$(this).addClass("active");
            		var index = $(this).index();
            		switch(index){
            			case 1: initJfsz(); break;
            			default: initJfye();
            		}
            	});
                initJfye();
            }
		});
	}
	//积分余额
    var jfyeParam = {
    		currPage: 1,
    		pageSize: Constants.page.pagesize
    };
	function initJfye(){
        var url = Constants.amUrlPath+"qzzfgl/getJfyePagination";
        var title = [], tds = [];
		var jfyeRactive = new Ractive({
			el: '#rhz',
            template: '#jfye-template',
            oncomplete: function () {
                
               //获取区域名称
                $.ajax({
        			url:Constants.amUrlPath+"common/getDqdmList",
        			type:"get",
        			success:function(xwList){
        				getSelectText(xwList, "code_QY");
        				setWDCode("code_QY", "-1");
        			}
            	});
            	//获取账户类型
                $.ajax({
        			url:Constants.amUrlPath+"common/getZhlxList",
        			type:"get",
        			success:function(xwList){
        				getSelectText(xwList, "code_ZHLX");
        				setWDCode("code_ZHLX", "-1");
        			}
            	});
                
				
                title = ['Account type','Region name','Available score'];
                tds = [{
                	name: 'zhlxDesc'
                },{
                	name: 'dqmc'
                },{
                	name: 'fz'
                }];
                drawTable("#wdJf-table", "#wdJf-pager", url, title, tds, jfyeParam);
                
            }
		});
		jfyeRactive.on({
        	search: function(){
        		jfyeParam.qymc = getWDCode("code_QY");
        		jfyeParam.zhlx = getWDCode("code_ZHLX");
        		drawTable("#wdJf-table", "#wdJf-pager", url, title, tds, jfyeParam);
        		jfyeParam = {
        	    		currPage: 1,
        	    		pageSize: Constants.page.pagesize
        	    }
        	}
        });
	}
	//积分收支
	function initJfsz(){
		var jfszRactive = new Ractive({
			el: '#rhz',
            template: '#jfsz-template',
            oncomplete: function () {
                initJfszRhzTab();
                $(".jfsz-tabs li").click(function() {
            		$(".jfsz-tabs li").removeClass("active");
            		$(this).addClass("active");
            		var index = $(this).index();
            		switch(index){
            			case 1: initJfszRhzAxwTab(); break;
            			case 2: initJfszHqmxTab(); break;
            			case 3: initJfszZcmxTab(); break;
            			default: initJfszRhzTab();
            		}
            	});
            }
		});
	}
	//积分-积分收支-日汇总
    var rhzParam = {
    		currPage: 1,
    		pageSize: Constants.page.pagesize
    };
	function initJfszRhzTab(){
        var url = Constants.amUrlPath+"qzzfgl/getJftjarPagination";
        var title = [], tds = [];
		var jfRhzRactive = new Ractive({
			el: '#rhzong',
            template: '#rhz-template',
            data: {
            	format: function(time){
            		var h = time.substring(0, 4);
            		var m = time.substring(4, 6);
            		var d = time.substring(6, 8);
            		return h + "-" + m + "-" + d;
            	}
            },
            oncomplete: function () {
                
            	title = ['Date', 'Score  acquisition ', 'Score  expenditure'];
            	tds = [{
            		name: "tjrq",
            		width: 10,
            		innerhtml: function(opts){
            			var d = moment(opts.tjrq).format("YYYY-MM-DD");
                		return d;
            		}
            	},{
            		name: "hqjf",
            		width: 10,
            		innerhtml: function(opts){
            			var d = moment(opts.tjrq).format("YYYY-MM-DD");
            			return "<a href='javascript:void(0)' onclick='zfgl_app.toHqmx(\""+d+"\")'>"+opts.hqjf+"</a>";
            		}
            	},{
            		name: "zcjf",
            		width: 10,
            		innerhtml: function(opts){
                		var d = moment(opts.tjrq).format("YYYY-MM-DD");
            			return "<a href='javascript:void(0)' onclick='zfgl_app.toZcmx(\""+d+"\")'>"+opts.zcjf+"</a>";
            		}
            	}]
            	drawTable("#wdRhz-table", "#wdRhz-pager", url, title, tds, rhzParam);
                
                //获取区域名称
            	$.ajax({
        			url:Constants.amUrlPath+"common/getDqdmList",
        			type:"get",
        			success:function(xwList){
        				getSelectText(xwList, "code_QY");
        			}
            	});
            }
		});

		jfRhzRactive.on({
        	search: function(){
        		rhzParam.startTime = jfRhzRactive.get("startDate");
        		rhzParam.endTime = jfRhzRactive.get("endDate");
        		rhzParam.szdq = getWDCode("code_QY");
        		if(!getWDCode("code_QY")){
        			alert("Select the region name!");
        			return;
        		}
        		drawTable("#wdRhz-table", "#wdRhz-pager", url, title, tds, rhzParam);
        		rhzParam = {
        	    		currPage: 1,
        	    		pageSize: Constants.page.pagesize
        	    }
        	}
        });
	          
	}
	function toHqmx(s){
		var param = {};
		param.currPage = 1,
		param.pageSize = Constants.page.pagesize
		param.startTime = s;
		param.endTime = s;
		param.szdq = getWDCode("code_QY");
		$(".jfsz-tabs li").removeClass("active");
		$(".jfsz-tabs li").eq(2).addClass("active");
		initJfszHqmxTab(param);
	}
	function toZcmx(s){
		var param = {};
		param.currPage = 1,
		param.pageSize = Constants.page.pagesize
		param.startTime = s;
		param.endTime = s;
		param.szdq = getWDCode("code_QY");
		$(".jfsz-tabs li").removeClass("active");
		$(".jfsz-tabs li").eq(3).addClass("active");
		initJfszZcmxTab(param);
	}
	
	//积分-积分收支-日汇总(按行为)
	var rhzxwParam = {
    		currPage: 1,
    		pageSize: Constants.page.pagesize
    };
	function initJfszRhzAxwTab(){
        var url = Constants.amUrlPath+"qzzfgl/getJftjaraxwPagination";
        var title = [], tds = [];
		var jfRhzxwRactive = new Ractive({
			el: '#rhzong',
            template: '#rhzaxw-template',
            oncomplete: function () {
            	title = ['Date', 'Score acquisition / expenditure behavior', 'Score acquisition / expenditure'];
            	tds = [{
            		name: "tjrq",
            		width: 10,
            		innerhtml: function(opts){
            			var d = moment(opts.tjrq).format("YYYY-MM-DD");
                		return d;
            		}
            	},{
            		name: "xwDesc",
            		width: 10
            	},{
            		name: "jfz",
            		width: 10
            	}]
            	drawTable("#wdRhzxw-table", "#wdRhzxw-pager", url, title, tds, rhzxwParam);
                
                //获取积分行为
            	$.ajax({
        			url:Constants.amUrlPath+"common/getXwList",
        			type:"get",
        			success:function(xwList){

        				var quanbu = [{
        					"code" : "-1",
        					"codedesc" : "All"
        				}];
        				xwList = $.merge(quanbu, xwList);
        				getSelectText(xwList, "code_JFXW");
        				setWDCode("code_JFXW", "-1");
        				
        			}
            	});
            	//获取区域名称
            	$.ajax({
        			url:Constants.amUrlPath+"common/getDqdmList",
        			type:"get",
        			success:function(xwList){
        				getSelectText(xwList, "code_QY");
        			}
            	});
            }
		});
		jfRhzxwRactive.on({
        	search: function(){
        		rhzxwParam.startTime = jfRhzxwRactive.get("startDate");
        		rhzxwParam.endTime = jfRhzxwRactive.get("endDate");
        		rhzxwParam.jfszxw = getWDCode("code_JFXW");
        		rhzxwParam.szdq = getWDCode("code_QY");
        		if(!getWDCode("code_QY")){
        			alert("Select the region name!");
        			return;
        		}
        		drawTable("#wdRhzxw-table", "#wdRhzxw-pager", url, title, tds, rhzxwParam);
        		rhzxwParam = {
        	    		currPage: 1,
        	    		pageSize: Constants.page.pagesize
        	    }
        	}
        });
	}
	//积分-积分收支-获取明细
	var hqmxParam;
	function initJfszHqmxTab(params){
		if(arguments.length < 1){
			hqmxParam = {
					currPage: 1,
	        		pageSize: Constants.page.pagesize
			};
		}else{
			hqmxParam = params; 
		}
        var url = Constants.amUrlPath+"qzzfgl/getJfhqmxPagination";
        var title, tds;
		var jsHqmxRactive = new Ractive({
			el: '#rhzong',
            template: '#hqmx-template',
            oncomplete: function () {
        		jsHqmxRactive.set("startDate", hqmxParam.startTime);
        		jsHqmxRactive.set("endDate", hqmxParam.endTime);
               //获取区域名称
            	$.ajax({
        			url:Constants.amUrlPath+"common/getDqdmList",
        			type:"get",
        			success:function(xwList){
        				getSelectText(xwList, "code_QY");
        			}
            	});

                title = ['Date', 'Score  acquisition ','Detailed description'];
                tds = [{
                	name: 'zcrq',
                	width: 164,
                	innerhtml: function(opts){
                		return moment(opts.zcrq).format("YYYY-MM-DD HH:mm:ss");
                	}
                },{
                	name: 'fz',
                	width: 164
                },{
                	name: 'bz',
                	width: 619,
                	float: 'left',
                	innerhtml: function(opts){
                		var sm = "";
                		if(opts.alljfzcfx == "14"){
                			sm="Refund(Order Number : "+(opts.bz==null?'':opts.bz)+") - Consumer : "+opts.mc;
                		}else {
                			sm=opts.alljfzcfxDesc
                		}
                		return sm;
                	}
                }];
                drawTable("#wdHqmx-table", "#wdHqmx-pager", url, title, tds, hqmxParam);
            }
		})
		jsHqmxRactive.on({
        	search: function(){
        		
        		hqmxParam.startTime = jsHqmxRactive.get("startDate");
        		hqmxParam.endTime = jsHqmxRactive.get("endDate");
        		hqmxParam.szdq = getWDCode("code_QY");
        		if(!getWDCode("code_QY")){
        			alert("Select the region name!");
        			return;
        		}
        		drawTable("#wdHqmx-table", "#wdHqmx-pager", url, title, tds, hqmxParam);
        		hqmxParam = {
    					currPage: 1,
    	        		pageSize: Constants.page.pagesize
    			}
        	}
        });
	}
	//积分-积分收支-支出明细
	var zcmxParam;
	function initJfszZcmxTab(params){
		if(arguments.length < 1){
			zcmxParam = {
					currPage: 1,
	        		pageSize: Constants.page.pagesize
			};
		}else{
			zcmxParam = params;
		}
        var url = Constants.amUrlPath+"qzzfgl/getJfzcmxPagination";
        var title, tds;
		var jfzcmxRactive = new Ractive({
			el: '#rhzong',
            template: '#zcmx-template',
            oncomplete: function () {
            	jfzcmxRactive.set("startDate", zcmxParam.startTime);
            	jfzcmxRactive.set("endDate", zcmxParam.endTime);
                
                //获取区域名称
            	$.ajax({
        			url:Constants.amUrlPath+"common/getDqdmList",
        			type:"get",
        			success:function(xwList){
        				getSelectText(xwList, "code_QY");
        			}
            	});
            	
                title = ['Date', 'Score expenditure', 'Detailed description'];
                tds = [{
                	width: 164,
                	innerhtml: function(opts){
                		return moment(opts.hqrq).format("YYYY-MM-DD HH:mm:ss");
                	}
                },{
                	width: 164,
                	name: 'fz'
                },{
                	width: 619,
                	name: 'bz',
                	float: 'left',
                	innerhtml:function(opts){
                		var sm="";
                		if(opts.allHqxw == "01"){
                			sm=opts.allHqxwDesc+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "02"){
                			sm=opts.allHqxwDesc+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "03"){
                			sm=opts.allHqxwDesc+" ( Product number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "04"){
                			sm=opts.allHqxwDesc+" ( "+jfzcmxRactive.get("i18n_zfgl_hzf")+" : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "05"){
                			sm=opts.allHqxwDesc+" ( Order Number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "06"){
                			sm=opts.allHqxwDesc+" ( Order Number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "07"){
                			sm=opts.allHqxwDesc+" ( Order Number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "08"){
                			sm="Score of comment post bill ( Order Number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "09"){
                			sm="Score of improve data -Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "10"){
                			sm="Score of share trading center home page-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "11"){
                			sm="Score of share product ( Product number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Consumer : "+opts.mc;
                		} else if(opts.allHqxw == "12"){
                			sm=opts.allHqxwDesc+" ( Consumer : "+(opts.bz==null?'':opts.bz)+" ) "+"-Partner : "+opts.mc;
                		} else if(opts.allHqxw == "13"){
                			sm=opts.allHqxwDesc+" ( Product number : "+(opts.bz==null?'':opts.bz)+" ) "+"-Partner : "+opts.mc;
                		}else if(opts.allHqxw=="14"){//逆向交易
        					sm=opts.allHqxwDesc+"(Order Number："+(opts.bz==null?'':opts.bz)+")";
        				}else if(opts.allHqxw=="15"){//积分兑换
        					sm=opts.allHqxwDesc+"(Exchange Number："+(opts.bz==null?'':opts.bz)+")";
        				}else if(opts.allHqxw=="16"){//积分抵扣
        					sm=opts.allHqxwDesc+"(Cash withdrawal number："+(opts.bz==null?'':opts.bz)+")";
        				}else {//
        					sm= opts.allHqxwDesc;
        				}
                		return sm;
                	}
                }];
                drawTable("#wdZcmx-table", "#wdZcmx-pager", url, title, tds, zcmxParam);
                
            }
		})
		jfzcmxRactive.on({
        	search: function(){
        		zcmxParam.startTime = jfzcmxRactive.get("startDate");
        		zcmxParam.endTime = jfzcmxRactive.get("endDate");
        		zcmxParam.szdq = getWDCode("code_QY");
        		if(!getWDCode("code_QY")){
        			alert("Select the region name!");
        			return;
        		}
        		drawTable("#wdZcmx-table", "#wdZcmx-pager", url, title, tds, zcmxParam);
        		zcmxParam = {
    					currPage: 1,
    	        		pageSize: Constants.page.pagesize
    			}
        	}
        });
	}
	//绘制表
	//ele 表选择器
	//url 接口地址
	//title 表格标题
	//tds 表格内容
	//param 查询条件
	function drawTable(ele, pageEle, url, title, tds, param){
		$(ele).wdGrid({
			url:url,
			colNames:title,
			colModel:tds,
	        dataType: "GET",
	        submitType: "noForm",
	        postData: param,
	        checkflg: false,
	        pager: pageEle,
	        rowList: Constants.page.rowList,
	        pagesize: Constants.page.pagesize
	        
		})
	}
	//下拉列表
	function getSelectText(data, ele){
		var eleEnd = ele.split("_")[1];
		
		store.set(eleEnd, JSON.stringify(data));
		selecttextInfo();
		setWDCode(ele, "000000");
		
	}
	return {
		init: function(){
			initJf();
		},
		toHqmx: toHqmx,
		toZcmx: toZcmx
	}
}();
$(document).ready(zfgl_app.init);